package com.example.innovate.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.SuperAdminInfoDO;
import com.example.innovate.domain.UserInfoDO;
import com.example.innovate.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class TokenServiceImpl implements TokenService {

    @Override
    public String getUserToken(UserInfoDO userInfoDO) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 5000 * 12;//一小时有效时间* 12
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(userInfoDO.getUserInfoName()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(userInfoDO.getUserInfoPassword()));
        return token;
    }

    @Override
    public String getAdminToken(AdminInfoDO adminInfoDO) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 5000 * 12;//一小时有效时间* 12
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(adminInfoDO.getAdminUserName()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(adminInfoDO.getAdminPassword()));
        return token;
    }

    @Override
    public String getSuperAdminToken(SuperAdminInfoDO superAdminInfoDO) {
        Date start = new Date();
        long currentTime = System.currentTimeMillis() + 60* 60 * 5000 * 12;//一小时有效时间* 12
        Date end = new Date(currentTime);
        String token = "";

        token = JWT.create().withAudience(superAdminInfoDO.getAdminUserName()).withIssuedAt(start).withExpiresAt(end)
                .sign(Algorithm.HMAC256(superAdminInfoDO.getAdminPassword()));
        return token;
    }


}
